package com.business_clean.app.service;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.business_clean.app.App;
import com.business_clean.app.network.NetUrl;
import com.business_clean.app.uitl.ToastUtil;
import com.business_clean.app.uitl.UploadFileHelper;
import com.business_clean.data.mode.leader.CustomMarkLeaderData;

import java.util.HashMap;
import java.util.List;

import io.reactivex.rxjava3.functions.Consumer;
import me.hgj.mvvmhelper.ext.LogExtKt;
import okhttp3.Response;
import rxhttp.wrapper.param.RxHttp;

/**
 * 水印图片、视频 后台上报用的工具，因为用 service 前台必须创建 1 个 Notification
 */
public class ServerReporter {
    private String TAG = "ServerReporter";

    private static ServerReporter instance;
    private Context mContext;

    private ServerReporter() {
        // 私有构造函数，避免外部实例化
    }

    public void initialize(Context context) {
        this.mContext = context.getApplicationContext();
    }


    public static synchronized ServerReporter getInstance() {
        if (instance == null) {
            instance = new ServerReporter();
        }
        return instance;
    }


    public void reportToServer(int act_type, int message_type, double lat, double lnt, String address,
                               String waterUrl, String original, String thumb,
                               List<CustomMarkLeaderData> attendance_data, List<CustomMarkLeaderData> cancel_overtime_data, long takePhotoMills) {
        if (mContext == null) {
            LogExtKt.logE("Context is null", TAG);
            return;
        }
        if (1 == message_type) {//图片
            if (!TextUtils.isEmpty(waterUrl) && !TextUtils.isEmpty(original)) {
                if (!TextUtils.isEmpty(waterUrl) && !TextUtils.isEmpty(original)) {
                    UploadFileHelper.getInstance().uploadPictures(mContext, waterUrl, UploadFileHelper.PATH_HEADER_CAMERA, new UploadFileHelper.UploadListener() {
                        @Override
                        public void onUploadSuccess(String waterResponse) {
                            UploadFileHelper.getInstance().uploadPictures(mContext, original, UploadFileHelper.PATH_HEADER_CAMERA, new UploadFileHelper.UploadListener() {
                                @Override
                                public void onUploadSuccess(String originalResponse) {
                                    UploadFileHelper.getInstance().uploadPictures(mContext, thumb, UploadFileHelper.PATH_HEADER_CAMERA, new UploadFileHelper.UploadListener() {
                                        @Override
                                        public void onUploadSuccess(String thumbResponse) {
                                            // 分别上传水印图片和原图成功后，再发送请求
                                            requestMark(act_type, message_type, lat, lnt, address, waterResponse, originalResponse, thumbResponse,
                                                    attendance_data, cancel_overtime_data, takePhotoMills);
                                        }

                                        @Override
                                        public void onUploadFailed(String error) {
                                            ToastUtil.show("上传原图失败" + error);
                                        }

                                        @Override
                                        public void onUploadProgress(int progress) {
                                            // 原图上传进度
                                        }
                                    });
                                }

                                @Override
                                public void onUploadFailed(String error) {
                                    ToastUtil.show("上传原图失败" + error);
                                }

                                @Override
                                public void onUploadProgress(int progress) {
                                    // 原图上传进度
                                }
                            });
                        }

                        @Override
                        public void onUploadFailed(String error) {
                            ToastUtil.show("上传水印图片失败" + error);
                        }

                        @Override
                        public void onUploadProgress(int progress) {
                            // 水印图片上传进度
                        }
                    });
                }
            }
        } else if (2 == message_type) {
            if (!TextUtils.isEmpty(waterUrl)) {//视频
                //先上传视频
                UploadFileHelper.getInstance().uploadPictures(mContext, waterUrl, UploadFileHelper.PATH_HEADER_CAMERA, new UploadFileHelper.UploadListener() {
                    @Override
                    public void onUploadSuccess(String response) {
                        //再上传缩略图
                        UploadFileHelper.getInstance().uploadPictures(mContext, thumb, UploadFileHelper.PATH_HEADER_CAMERA, new UploadFileHelper.UploadListener() {
                            @Override
                            public void onUploadSuccess(String thumbResponse) {
                                requestMark(act_type, message_type, lat, lnt, address, response, null, thumbResponse,
                                        attendance_data, cancel_overtime_data, takePhotoMills);
                            }

                            @Override
                            public void onUploadFailed(String error) {
                            }

                            @Override
                            public void onUploadProgress(int progress) {
                            }
                        });
                    }

                    @Override
                    public void onUploadFailed(String error) {
                        ToastUtil.show("上传失败" + error);
                    }

                    @Override
                    public void onUploadProgress(int progress) {

                    }
                });
            }
        }
    }


    /**
     * 标记上报
     */
    private void requestMark(int act_type, int message_type, double lat, double lnt, String address, String waterUrl,
                             String original, String thumb, List<CustomMarkLeaderData> attendance_data, List<CustomMarkLeaderData>
                                     cancel_overtime_data, Long takePhotoMills) {
//        LogExtKt.logE("当前的内容:" + message_type, "");
//        LogExtKt.logE("标记图片水印地址:" + waterUrl, "");
//        LogExtKt.logE("标记图片人员:" + attendance_data.size(), "");
//        LogExtKt.logE("标记图片人员json:" + JSON.toJSONString(attendance_data), "");
//        LogExtKt.logE("标记图片取消人员:" + cancel_overtime_data.size(), "");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("act_type", "" + act_type);//操作类型 1日常工作 2领班打卡
        hashMap.put("message_type", "" + message_type);//消息类型 1图片消息 2视频
        hashMap.put("origin_media_url", "" + original);//消息媒体地址(无水印)
        if (1 == message_type) {//区分类别
            hashMap.put("pic_thumb", "" + thumb);//图片缩略图 图片消息必传
        } else {
            hashMap.put("video_cover_url", "" + thumb);//消息媒体地址(无水印)
        }
        hashMap.put("project_uuid", App.appViewModelInstance.getProjectInfo().getValue().getUuid());
        hashMap.put("media_url", "" + waterUrl);//消息媒体地址(有水印)
        hashMap.put("address", "" + address);
        hashMap.put("lnt", "" + lnt);
        hashMap.put("lat", "" + lat);
        hashMap.put("take_photo_time", "" + (takePhotoMills / 1000));//拍照时间
        hashMap.put("security_code", "" + takePhotoMills);//防伪码
        if (attendance_data != null && attendance_data.size() > 0) {
            hashMap.put("attendance_data", JSON.toJSONString(attendance_data));
        }
        if (cancel_overtime_data != null && cancel_overtime_data.size() > 0) {
            hashMap.put("cancel_overtime_data", JSON.toJSONString(cancel_overtime_data));
        }
        RxHttp.get(NetUrl.MARK_PHOTO).addAll(hashMap).asOkResponse().subscribe(new Consumer<Response>() {
            @Override
            public void accept(Response response) throws Throwable {

            }
        });
    }
}